package com.hzit.seckill.service.impl;

import com.alibaba.fastjson.JSON;
import com.hzit.common.utils.MyDate;
import com.hzit.seckill.entity.SeckillGoods;
import com.hzit.seckill.mapper.SeckillGoodsMapper;
import com.hzit.seckill.service.ISeckillGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hbxfwf
 * @since 2023-10-12
 */
@Service
public class SeckillGoodsServiceImpl extends ServiceImpl<SeckillGoodsMapper, SeckillGoods> implements ISeckillGoodsService {

	@Autowired
	private StringRedisTemplate redisTemplate;


	//1. 从redis中得到秒杀商品
	@Override
	public List<SeckillGoods> getSeckillGoodsFromRedis() {
		//1.1 得到秒杀商品的字符串集合
		Date date = MyDate.getDateMenus().get(0);
		String dateStr = MyDate.getDateStr(date);
		HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
		List<String> values = hashOperations.values("SeckillGoods_" + dateStr);
		//1.2 遍历values集合转换为List<SeckillGoods>集合
		List<SeckillGoods> collect = values.stream().map(m -> JSON.parseObject(m, SeckillGoods.class)).collect(Collectors.toList());
		return collect;
	}

	public static void main(String[] args) {
		Date date = MyDate.getDateMenus().get(0);
		String dateStr = MyDate.getDateStr(date);
		System.out.println("dateStr = " + dateStr);

	}
}
